package com.returnsoft.softphone.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.ejb.Stateless;

import com.returnsoft.softphone.dto.AgentDto;
import com.returnsoft.softphone.dto.CampaignDto;
import com.returnsoft.softphone.dto.PeerDto;
import com.returnsoft.softphone.dto.PeerStateDto;
import com.returnsoft.softphone.dto.QueueDto;
import com.returnsoft.softphone.dto.ServerDto;
import com.returnsoft.softphone.dto.SessionDto;
import com.returnsoft.softphone.dto.SessionTypeDto;
import com.returnsoft.softphone.dto.SupervisorDto;
import com.returnsoft.softphone.dto.TransferDto;
import com.returnsoft.softphone.entity.Agent;
import com.returnsoft.softphone.entity.Campaign;
import com.returnsoft.softphone.entity.Peer;
import com.returnsoft.softphone.entity.PeerState;
import com.returnsoft.softphone.entity.Queue;
import com.returnsoft.softphone.entity.Server;
import com.returnsoft.softphone.entity.Session;
import com.returnsoft.softphone.entity.SessionType;
import com.returnsoft.softphone.entity.Supervisor;
import com.returnsoft.softphone.entity.Transfer;
import com.returnsoft.softphone.exception.ConversionExcepcion;

@Stateless
public class Conversion {

	public Session toSession(SessionDto sessionDto) throws ConversionExcepcion {
		try {
			Session session = new Session();
			session.setId(sessionDto.getId());
			session.setStartedAt(sessionDto.getStartedAt());
			session.setEndedAt(sessionDto.getEndedAt());
			if (sessionDto.getAgent() != null) {
				Agent agent = new Agent();
				agent.setId(sessionDto.getAgent().getId());
				session.setAgent(agent);
			}
			if (sessionDto.getCampaign() != null) {
				Campaign campaign = new Campaign();
				campaign.setId(sessionDto.getCampaign().getId());
				session.setCampaign(campaign);
			}
			if (sessionDto.getSessionType() != null) {
				SessionType sessionType = new SessionType();
				sessionType.setId(sessionDto.getSessionType().getId());
				session.setSessionType(sessionType);
			}
			if (sessionDto.getSession() != null) {
				Session sessionOld = new Session();
				sessionOld.setId(sessionDto.getSession().getId());
				session.setSession(sessionOld);
			}

			return session;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public SessionDto fromSession(Session session) throws ConversionExcepcion {
		try {
			SessionDto sessionDto = new SessionDto();
			sessionDto.setId(session.getId());
			sessionDto.setHost(session.getHost());
			sessionDto.setStartedAt(session.getStartedAt());
			sessionDto.setEndedAt(session.getEndedAt());
			sessionDto.setPeer(session.getPeer());

			if (session.getAgent() != null) {
				AgentDto agentDto = new AgentDto();
				agentDto.setId(session.getAgent().getId());
				agentDto.setCode(session.getAgent().getCode());
				sessionDto.setAgent(agentDto);
			}
			if (session.getCampaign() != null) {
				CampaignDto campaignDto = new CampaignDto();
				campaignDto.setId(session.getCampaign().getId());
				campaignDto.setName(session.getCampaign().getName());
				sessionDto.setCampaign(campaignDto);
			}
			if (session.getSessionType() != null) {
				SessionTypeDto sessionTypeDto = new SessionTypeDto();
				sessionTypeDto.setId(session.getSessionType().getId());
				sessionTypeDto.setCode(session.getSessionType().getCode());
				sessionTypeDto.setName(session.getSessionType().getName());
				sessionDto.setSessionType(sessionTypeDto);
			}
			if (session.getSession() != null) {
				SessionDto sessionOldDto = new SessionDto();
				sessionOldDto.setId(session.getSession().getId());
				sessionDto.setSession(sessionOldDto);
			}

			return sessionDto;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public SessionTypeDto fromSessionType(SessionType sessionType)
			throws ConversionExcepcion {

		try {
			SessionTypeDto sessionTypeDto = new SessionTypeDto();
			sessionTypeDto.setId(sessionType.getId());
			sessionTypeDto.setCode(sessionType.getCode());
			sessionTypeDto.setName(sessionType.getName());
			return sessionTypeDto;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public TransferDto fromTransfer(Transfer transfer)
			throws ConversionExcepcion {

		try {
			TransferDto transferDto = new TransferDto();
			transferDto.setId(transfer.getId());
			transferDto.setName(transfer.getName());
			transferDto.setNumber(transfer.getNumber());
			return transferDto;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public CampaignDto fromCampaign(Campaign campaign)
			throws ConversionExcepcion {
		try {
			CampaignDto campaignDto = new CampaignDto();
			campaignDto.setId(campaign.getId());
			campaignDto.setName(campaign.getName());
			campaignDto.setCode(campaign.getCode());
			if (campaign.getServer()!=null && campaign.getServer().getId()>0) {
				ServerDto serverDto = fromServer(campaign.getServer());
				campaignDto.setServer(serverDto);
			}
			return campaignDto;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public QueueDto fromQueue(Queue queue) throws ConversionExcepcion {

		try {
			QueueDto queueDto = new QueueDto();
			queueDto.setId(queue.getId());
			queueDto.setName(queue.getName());
			return queueDto;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public Agent toAgent(AgentDto agentDto) throws ConversionExcepcion {

		try {
			Agent agent = new Agent();
			agent.setId(agentDto.getId());
			agent.setCode(agentDto.getCode());
			agent.setUserId(agentDto.getUserId());
			agent.setAutoready(agentDto.getAutoready());

			if (agentDto.getCampaigns() != null
					&& agentDto.getCampaigns().size() > 0) {
				Set<Campaign> campaigns = new HashSet<Campaign>();
				for (CampaignDto campaignDto : agentDto.getCampaigns()) {
					Campaign campaign = new Campaign();
					campaign.setId(campaignDto.getId());
					campaigns.add(campaign);
				}
				agent.setCampaigns(campaigns);
			}

			if (agentDto.getPeer() != null) {
				Peer peer = new Peer();
				peer.setId(agentDto.getPeer().getId());
				agent.setPeer(peer);
			}

			if (agentDto.getSupervisor() != null) {
				Supervisor supervisor = new Supervisor();
				supervisor.setId(agentDto.getSupervisor().getId());
				agent.setSupervisor(supervisor);
			}

			return agent;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public Supervisor toSupervisor(SupervisorDto supervisorDto)
			throws ConversionExcepcion {

		try {
			Supervisor supervisor = new Supervisor();
			supervisor.setId(supervisorDto.getId());
			supervisor.setUserId(supervisorDto.getUserId());
			supervisor.setCode(supervisorDto.getCode());
			if (supervisorDto.getCampaigns() != null
					&& supervisorDto.getCampaigns().size() > 0) {
				Set<Campaign> campaigns = new HashSet<Campaign>();
				for (CampaignDto campaignDto : supervisorDto.getCampaigns()) {
					Campaign campaign = new Campaign();
					campaign.setId(campaignDto.getId());
					campaigns.add(campaign);
				}
				supervisor.setCampaigns(campaigns);
			}

			return supervisor;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	/*public Agent toAgentWithCampaigns(AgentDto agentDto)
			throws ConversionExcepcion {

		try {
			Agent agent = new Agent();
			agent.setId(agentDto.getId());
			agent.setCode(agentDto.getCode());
			agent.setUserId(agentDto.getUserId());
			agent.setAutoready(agentDto.getAutoready());
			if (agentDto.getCampaigns() != null
					&& agentDto.getCampaigns().size() > 0) {
				Set<Campaign> campaigns = new HashSet<Campaign>();
				for (CampaignDto campaignDto : agentDto.getCampaigns()) {
					Campaign campaign = new Campaign();
					campaign.setId(campaignDto.getId());
					campaigns.add(campaign);
				}
				agent.setCampaigns(campaigns);
			}
			if (agentDto.getPeer() != null) {
				Peer peer = new Peer();
				peer.setId(agentDto.getPeer().getId());
				agent.setPeer(peer);
			}
			if (agentDto.getSupervisor() != null) {
				Supervisor supervisor = new Supervisor();
				supervisor.setId(agentDto.getSupervisor().getId());
				agent.setSupervisor(supervisor);
			}

			return agent;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}*/

	public SupervisorDto fromSupervisor(Supervisor supervisor)
			throws ConversionExcepcion {
		try {
			System.out.println("Ingreso a fromSupervisor");
			System.out.println(supervisor.getId());
			
			SupervisorDto supervisorDto = new SupervisorDto();
			supervisorDto.setId(supervisor.getId());
			supervisorDto.setCode(supervisor.getCode());
			if (supervisor.getCampaigns() != null
					&& supervisor.getCampaigns().size() > 0) {
				System.out.println(supervisor.getCampaigns().size());
				List<CampaignDto> campaignsDto = new ArrayList<CampaignDto>();
				for (Campaign campaign : supervisor.getCampaigns()) {
					CampaignDto campaignDto = fromCampaign(campaign);
					campaignsDto.add(campaignDto);
				}
				supervisorDto.setCampaigns(campaignsDto);
			}else{
				System.out.println("Si es nuloo");
			}
			return supervisorDto;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public AgentDto fromAgent(Agent agent) throws ConversionExcepcion {

		try {
			AgentDto agentDto = new AgentDto();
			agentDto.setId(agent.getId());
			agentDto.setCode(agent.getCode());
			agentDto.setUserId(agent.getUserId());
			agentDto.setAutoready(agent.getAutoready());

			if (agent.getPeer() != null) {
				PeerDto peerDto = new PeerDto();
				peerDto.setId(agent.getPeer().getId());
				agentDto.setPeer(peerDto);
			}

			if (agent.getCampaigns() != null && agent.getCampaigns().size() > 0) {
				List<CampaignDto> campaignsDto = new ArrayList<CampaignDto>();
				for (Campaign campaign : agent.getCampaigns()) {
					CampaignDto campaignDto = fromCampaign(campaign);
					campaignsDto.add(campaignDto);
				}
				agentDto.setCampaigns(campaignsDto);
			}

			if (agent.getSupervisor() != null && agent.getSupervisor().getId()>0) {
				SupervisorDto supervisorDto = new SupervisorDto();
				supervisorDto.setId(agent.getSupervisor().getId());
				agentDto.setSupervisor(supervisorDto);
			}

			return agentDto;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	/*public AgentDto fromAgentWithPeer(Agent agent) throws ConversionExcepcion {

		try {
			AgentDto agentDto = new AgentDto();
			agentDto.setId(agent.getId());
			agentDto.setCode(agent.getCode());
			agentDto.setUserId(agent.getUserId());
			agentDto.setAutoready(agent.getAutoready());

			if (agent.getPeer() != null) {
				PeerDto peerDto = new PeerDto();
				peerDto.setId(agent.getPeer().getId());
				peerDto.setName(agent.getPeer().getName());
				peerDto.setPassword(agent.getPeer().getPassword());
				peerDto.setProtocol(agent.getPeer().getProtocol());
				if (agent.getPeer().getServer() != null) {
					ServerDto serverDto = new ServerDto();
					serverDto.setId(agent.getPeer().getServer().getId());
					peerDto.setServer(serverDto);
				}
				agentDto.setPeer(peerDto);
			}

			return agentDto;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}*/

	public PeerStateDto fromPeerState(PeerState peerState)
			throws ConversionExcepcion {

		try {
			PeerStateDto peerStateDto = new PeerStateDto();
			peerStateDto.setId(peerState.getId());
			peerStateDto.setName(peerState.getName());
			peerStateDto.setHost(peerState.getHost());
			peerStateDto.setCreatedAt(peerState.getCreatedAt());
			peerStateDto.setNumber(peerState.getNumber());
			peerStateDto.setState(peerState.getState());

			if (peerState.getServer() != null) {
				ServerDto serverDto = new ServerDto();
				serverDto.setId(peerState.getServer().getId());
				peerStateDto.setServer(serverDto);
			}

			if (peerState.getAgent() != null) {
				AgentDto agentDto = new AgentDto();
				agentDto.setId(peerState.getAgent().getId());
				peerStateDto.setAgent(agentDto);
			}

			return peerStateDto;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public PeerState toPeerState(PeerStateDto peerStateDto)
			throws ConversionExcepcion {

		try {
			PeerState peerState = new PeerState();
			peerState.setId(peerStateDto.getId());
			peerState.setName(peerStateDto.getName());
			peerState.setHost(peerStateDto.getHost());
			peerState.setCreatedAt(peerStateDto.getCreatedAt());
			peerState.setNumber(peerStateDto.getNumber());
			peerState.setState(peerStateDto.getState());

			if (peerStateDto.getServer() != null) {
				Server server = new Server();
				server.setId(peerStateDto.getServer().getId());
				peerState.setServer(server);
			}

			return peerState;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public Peer toPeer(PeerDto peerDto) throws ConversionExcepcion {

		try {
			Peer peer = new Peer();
			peer.setId(peerDto.getId());
			peer.setName(peerDto.getName());
			peer.setPassword(peerDto.getPassword());
			if (peerDto.getServer() != null) {
				ServerDto serverDto = peerDto.getServer();
				Server server = new Server();
				server.setId(serverDto.getId());
				peer.setServer(server);
			}
			if (peerDto.getAgent() != null) {
				Agent agent = new Agent();
				agent.setId(peerDto.getAgent().getId());
				peer.setAgent(agent);
			}

			return peer;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public PeerDto fromPeer(Peer peer) throws ConversionExcepcion {

		try {
			PeerDto peerDto = new PeerDto();
			peerDto.setId(peer.getId());
			peerDto.setName(peer.getName());
			peerDto.setPassword(peer.getPassword());
			if (peer.getServer() != null) {
				Server server = peer.getServer();
				ServerDto serverDto = new ServerDto();
				serverDto.setId(server.getId());
				serverDto.setIp(server.getIp());
				serverDto.setAmiPassword(server.getAmiPassword());
				serverDto.setAmiPort(server.getAmiPort());
				serverDto.setAmiUser(server.getAmiUser());
				serverDto.setName(server.getName());
				peerDto.setServer(serverDto);
			}
			if (peer.getAgent() != null) {
				Agent agent = peer.getAgent();
				AgentDto agentDto = new AgentDto();
				agentDto.setId(agent.getId());
				peerDto.setAgent(agentDto);
			}

			return peerDto;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public ServerDto fromServer(Server server) throws ConversionExcepcion {

		try {
			ServerDto serverDto = new ServerDto();
			serverDto.setId(server.getId());
			serverDto.setIp(server.getIp());
			serverDto.setCode(server.getCode());
			serverDto.setName(server.getName());
			serverDto.setAmiUser(server.getAmiUser());
			serverDto.setAmiPort(server.getAmiPort());
			serverDto.setAmiPassword(server.getAmiPassword());
			serverDto.setPbxDatabase(server.getPbxDatabase());

			return serverDto;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

}
